define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'adminlte', 'template'], function ($, undefined, Backend, Table, Form, Adminlte, Template) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'dormitory/bed/index',
                    add_url: 'dormitory/bed/add',
                    edit_url: 'dormitory/bed/edit',
                    del_url: 'dormitory/bed/del',
                    multi_url: 'dormitory/bed/multi',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                columns: [
                    [

                        {field: 'state', checkbox: true, formatter:function(value,row,index){//设置满足条件的行可以使用复选框
                                if(row.status == '2'){
                                    return {
                                        disabled : true
                                    }
                                }
                            }},
                        {field: 'id', title: __('ID'), operate: false, visible: false},
                        // {
                        //     field: 'dorm_number', title: __('Garden'), searchList: function (column) {
                        //         return Template('gardenTpl', {});
                        //     }, formatter: function (value, row, index) {
                        //         return '无';
                        //     }, visible: false
                        // },
                        {field: 'number', title: __('Number'), operate: false},
                        {field: 'a.number', title: __('Number'), operate: "LIKE %...%", visible: false},
                        {field: 'dorm_name', title: __('Dorm Name'), operate: false},
                        {field: 'dorm_floor', title: __('Floor'), operate: false, formatter: Controller.api.formatter.floor},
                        // {field: 'garden_name', title: __('Garden Name'), operate: false},
                        // {field: 'building_name', title: __('Building Name'), operate: false},
                        // {field: 'student_name', title: __('Student Name'), operate: false},
                        // {field: 'student_number', title: __('学号'), operate: false},
                        // {field: 'student_id_number', title: __('Student Id Number'), operate: false},
                        // {field: 'faculty_name', title: __('Faculty Name'), operate: false},
                        {field: 'status_text', title: __('Status'), operate: false},
                        {field: 'b.dorm_name', title: __('Dorm Name'), visible: false},
                        // {
                        //     field: 'b.type',
                        //     title: __('Type'),
                        //     align: 'left',
                        //     searchList: $.getJSON('dormitory/dorm/typeList?type=1'),
                        //     visible: false
                        // },
                        {field: 'e.number', title: __('Student Number'), visible: false},
                        {field: 'e.id_number', title: __('Student Id Number'), visible: false},
                        {
                            field: 'a.status',
                            title: __('Status'),
                            align: 'left',
                            searchList: $.getJSON('dormitory/bed/statusList'),
                            visible: false
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (value, row, index) {
                                if (row.status == '2') {
                                    return '';
                                }
                                return Table.api.formatter.operate.call(this, value, row, index);
                            }
                        },
                    ]
                ],
                commonSearch: true,
                searchFormVisible: true,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 监听下拉列表改变的事件
            $(document).on('change', 'select[name=garden_number]', function () {
                $("#garden_number").val($(this).val());
            });
            // 监听下拉列表改变的事件
            $(document).on('change', 'select[name=building_number]', function () {
                $("#building_number").val($(this).val());
            });

            // 生成床位号
            $(document).on("click", ".btn-init", function () {
                //在table外不可以使用添加.btn-change的方法
                //只能自己调用Table.api.multi实现
                //如果操作全部则ids可以置为空
                var ids = Table.api.selectedids(table);
                Table.api.multi("changestatus", ids.join(","), table, this);
            });


        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            formatter: {
                floor: function (value, row, index) {
                    return value + __('Floor Tips');
                },
                capacity: function (value, row, index) {
                    return row.bed_count + '/' + value;
                },
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});